package com.zxf.douban.util;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

public class ImageLoadTask extends AsyncTask<String, Void, Bitmap> {

	private ImageLoadCallback callback;

	public ImageLoadTask(ImageLoadCallback callback) {
		super();
		this.callback = callback;
	}

	public interface ImageLoadCallback {
		void beforeImageLoad();

		void afterImageLoad(Bitmap bitmap);
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		callback.beforeImageLoad();
	}

	@Override
	protected void onPostExecute(Bitmap result) {
		super.onPostExecute(result);
		callback.afterImageLoad(result);
	}

	@Override
	protected Bitmap doInBackground(String... params) {
		String imagePath = params[0];
		try {
			URL url = new URL(imagePath);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			InputStream is = conn.getInputStream();
			Bitmap bitmap = BitmapFactory.decodeStream(is);
			return bitmap;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
